#include "head.h"

int main(int argc, char *argv[])
{
    // ./client 192.168.118.128 1234
    ARGS_CHECK(argc,3);

    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(sockfd,-1,"socket");

    struct sockaddr_in serverAddr;
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(atoi(argv[2]));
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr *)&serverAddr, sizeof(serverAddr));
    //NETDISK_LOG_DEBUG(ret,-1);
    NETDISK_LOG_DEBUG(ret,-1,"connect");

    int epfd = epoll_create(1);
    NETDISK_LOG_DEBUG(ret,-1,"epoll_create");

    epollAdd(epfd,STDIN_FILENO);
    epollAdd(epfd,sockfd);

    printf("Please enter your username: "); 
    fflush(stdout);
    char username[256];
    scanf("%s",username);

    printf("Please enter your userpasswd: "); 
    fflush(stdout);
    char userpasswd[256];
    scanf("%s",userpasswd);
    while(1)
    {
        int ret = Login(username,userpasswd,sockfd);
        printf("Login ret = %d\n",ret);
        if(ret == 0)
        {
            break;
        }
        printf("Wrong Password!\n");
        printf("Please enter your userpasswd: "); 
        fflush(stdout);
        bzero(userpasswd,sizeof(userpasswd));
        scanf("%s",userpasswd);
    }

    train_t train;
    train.length = strlen(username);
    strcpy(train.data,username);
    send(sockfd,&train,sizeof(int)+train.length,MSG_NOSIGNAL);

    int length;
    char pwd[256] = {0};
    recv(sockfd,&length,sizeof(int),MSG_WAITALL);
    recv(sockfd,pwd,length,MSG_WAITALL);
    printf("%s\n",pwd);

    int orderflag;
    struct epoll_event readySet[5];

    while(1)
    {
        printf("> ");
        fflush(stdout);

        char path[256] = {0};
        char buf[1024] = {0};
        int readyNum = epoll_wait(epfd,readySet,5,-1);
        for(int i = 0; i < readyNum; ++i)
        {//客户端输入了命令
            if(readySet[i].data.fd == STDIN_FILENO)
            {
                ssize_t sret = read(STDIN_FILENO,buf,sizeof(buf));
                buf[sret-1] = '\0';
                time_t now;
                now=time(NULL);
                NETDISK_LOG_INFO(buf,now);
                //解析命令
                int ret = whichOrder(buf,&orderflag,path);
                printf("ret = %d\n",ret);
                printf("path = %s\n",path);
                if(ret == 0)
                {
                    //命令合法，发送请求给服务端
                    transOrder(sockfd,orderflag,path);
                    printf("path=%s\n",path);
                    if(orderflag==PUTS){
                        strcat(pwd,"/");
                        //path[strlen(path)]='\0';
                        strcat(pwd,path);
                        puts_client(sockfd,pwd,path);
                        break;
                    }
                    if(orderflag==GETS2){
                        printf("path is %s\n",path);
                        printf("gets_server2 has passed the switch\n");
                        gets_client2(sockfd,path);
                        break;
                    }
                }}

            else
            {//服务端发送了消息
                switch(orderflag)
                {
                case CD:break;
                case LS:
                        lsRecv(sockfd);
                        break;
                case PUTS:break;

                case GETS:
                          printf("gets_server has passed the switch");
                          gets_client(sockfd);
                          break;

                case REMOVE:break;
                case PWD:
                            pwdRecv(sockfd);
                            break;
                case MKDIR:break;
                case RMDIR:break;
                }
            }
        }
    }

    close(sockfd);

    return 0;
}
